<?php
/**
 * Description of orderController
 *
 * @author Henson
 */
class orderController{
	
    public function indexAction()
    {
		$order = new Order();
		if(_checkSuperLogin() && $order->checkPermission())
		{
			$totalNum = $order->getOrderTotal();
			$p = isset($_GET['p']) ? intval($_GET['p']) : 1;
			$pageUrl = '/index.php?r=order';
			$page = new Page($p, $pageUrl, $totalNum, $order->pageRow);
			$page->run();
			$rows = $order->getOrderList($p);
			foreach($rows as $key=>$value)
			{
				$rows[$key] = $order->dealOrder($value);
			}
			$pageInfo['rows'] = $rows;
			$pageInfo['page'] = $page;
			$view = new view(); 
			$view->showPage('management/orderList.tpl.htm', $pageInfo);
		}
    }
	
	public function genAction()
	{
		global $_CFG;
		if(_checkLogin())
		{
			$eid = intval($_POST['eid']);
			$number = intval($_POST['number']);
			$tids = @htmlspecialchars(trim($_POST['tids'], ","), ENT_QUOTES);
			$user = new User();
			$uid = $_COOKIE['uid'];
			$event = new Event();
			$eventInfo = $event->getEventByEid($eid);
			if($number < $eventInfo['min'])
			{
				echo 'minmum is '.$eventInfo['min'];
				die;
			}
			if($eventInfo['start_time'] - time() < $eventInfo['limit_time'] * 3600)
			{
				echo '停止预订';
				die;
			}
			if($eventInfo['cancel'] == '1')
			{
				echo '已取消';
				die;
			}
			$userInfo = $user->getUserByUid($uid);
			if($eid > 0 && ((preg_match("#^[0-9,]+$#", $tids) && $tids != "") || $tids == '') )
			{
				$table = new Table();
				if($table->checkValidTable($eid, $number, $tids))
				{
					$orderInsertArr = array();
					$orderInsertArr['eid'] = $eid;
					$orderInsertArr['tids'] = $tids;
					$orderInsertArr['uid'] = $_COOKIE['uid'];
					$orderInsertArr['username'] = $_COOKIE['username'];
					$orderInsertArr['number'] = $number;
					if($table->reserveTable($orderInsertArr, $tids))
					{
						echo "success:".$_CFG['msg'];die;
					}
					else
					{
						echo 'table has been reserved';die;
					}
				}
				else
				{
					echo $_CFG['msg'];
					$_CFG['msg'] = '';
					die;
				}
			}
			else
			{
				 echo 'data is invalid';die;
			}
		}
		else
		{
			 echo 'login first';die;
		}
	}
	
	function myAction()
	{
		if(_checkLogin())
		{
			$uid = $_COOKIE['uid'];
			$now = time();
			$order = new Order();
			$currentOrders = $order->getCurrentOrderByUid($uid);
			$event = new Event();
			foreach ($currentOrders as $key=>$value)
			{
				$currentOrders[$key] = $event->dealEvent($value);
			}
			$pageInfo['currentOrders'] = $currentOrders;
			$p = isset($_GET['p']) ? intval($_GET['p']) : 1;
			$pageUrl = '/index.php?r=event/my';
			$where = "where o.del='0' and (e.end_time < {$now} or o.cancel='1') and o.uid=".$uid;
			$totalNum = $order->getOldOrderTotal($where);
			$page = new Page($p, $pageUrl, $totalNum, $order->pageRow);
			$page->run();
			$orderBy = "order by o.id desc";
			$limit = " limit ".($p -1 ) * $order->pageRow .",".$order->pageRow;
			$oldOrders = $order->getOldOrderList($where, $orderBy, $limit);
			foreach($oldOrders as $key=>$value)
			{
				$oldOrders[$key] = $order->dealOrder($value);
			}
			$pageInfo['oldOrders'] = $oldOrders;
			$pageInfo['page'] = $page;
			$view = new View();
			$view->showPage("my_orders.tpl.htm", $pageInfo);
		}
		else
		{
			_showMessage("请先登陆", "/login.html");
		}
	}
	
	public function cancelAction()
	{
		if(_checkLogin())
		{
			$uid = $_COOKIE['uid'];
			$oid = intval($_POST['oid']);
			$eid = intval($_POST['eid']);
			$order = new Order();
			$orderOldInfo = $order->getOrderByOid($oid);
			if($orderOldInfo['start_time'] - time() < $orderOldInfo['limit_time'] * 3600)
			{
				echo '无法取消';
				die;
			}
			$updateArr = array();
			$updateArr['cancel'] = '1';
			$updateArr['cancel_time'] = time();
			if($order->updateOrder($updateArr, $oid, $eid))
			{
				if($orderOldInfo['tids'] != "")
				{
					$table = new Table();
					$table->cancelTables($orderOldInfo['tids']);
				}
				echo "success";die;
			}
		}
		else
		{
			_showMessage("请先登陆", "/login.html");
		}
	}
}

?>
